package com.gxa.admin.controller;

import com.gxa.admin.pojo.dto.EmpAddDto;
import com.gxa.admin.pojo.dto.EmpUpdateDto;
import com.gxa.admin.service.impl.EmpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 针对系统用户的增删改接口,只对admin用户开放
 * @author IKE <hp1041735092@outlook.jp>
 * @date 22/3/2024
 */

@RestController
@RequestMapping("/admin/employee")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(tags = "后台系统crud")
@Slf4j
public class EmpController {
    private final EmpService empService;

    @PostMapping("/addEmployee")
    @PreAuthorize("hasAnyAuthority('EMPMANA')")    //指定操作需要的权限
    @ApiOperation("添加员工")
    public ResponseEntity<Void> add(@RequestBody @Valid EmpAddDto empAddDto){
        empService.save(empAddDto);
        return ResponseEntity.ok().build();
    }


    /**
     * 使用redis存储token时,必须要在删除用户的同时使token失效
     */
    @PostMapping("/deleteEmployeeById")
    @PreAuthorize("hasAnyRole('ROLE_SADMIN')")
    @ApiOperation("根据id删除员工")
    public ResponseEntity<Void> delete(@RequestParam Integer employeeId){
        empService.delete(employeeId);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改员工信息
     */
    @ApiOperation("修改员工信息")
    @PreAuthorize("hasAnyRole('ROLE_SADMIN')")
    @PostMapping("/updateEmployeeMsg")
    public ResponseEntity<Void> edit(@RequestBody @Valid EmpUpdateDto empUpdateDto){
        empService.update(empUpdateDto);
        return ResponseEntity.ok().build();
    }
}
